home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / netprog.zip / NETPROG.TAR / ipc / sub_mesgmine.c < prev    next >
C/C++ Source or Header  |  1989-12-17  |  1KB  |  62 lines

  1. #include    "mesg.h"
  2.  
  3. /*
  4.  * Send a message by writing on a file descriptor.
  5.  * The mesg_len, mesg_type and mesg_data fields must be filled
  6.  * in by the caller.
  7.  */
  8.  
  9. mesg_send(fd, mesgptr)
  10. int    fd;
  11. Mesg    *mesgptr;
  12. {
  13.     int    n;
  14.  
  15.     /*
  16.      * Write the message header and the optional data.
  17.      * First calculate the length of the length field, plus the
  18.      * type field, plus the optional data.
  19.      */
  20.  
  21.     n = MESGHDRSIZE + mesgptr->mesg_len;
  22.  
  23.     if (write(fd, (char *) mesgptr, n) != n)
  24.         err_sys("message write error");
  25. }
  26.  
  27. /*
  28.  * Receive a message by reading on a file descriptor.
  29.  * Fill in the mesg_len, mesg_type and mesg_data fields, and return
  30.  * mesg_len as the return value also.
  31.  */
  32.  
  33. int
  34. mesg_recv(fd, mesgptr)
  35. int    fd;
  36. Mesg    *mesgptr;
  37. {
  38.     int    n;
  39.  
  40.     /*
  41.      * Read the message header first.  This tells us how much
  42.      * data follows the message for the next read.
  43.      * An end-of-file on the file descriptor causes us to
  44.      * return 0.  Hence, we force the assumption on the caller
  45.      * that a 0-length message means EOF.
  46.      */
  47.  
  48.     if ( (n = read(fd, (char *) mesgptr, MESGHDRSIZE)) == 0)
  49.         return(0);        /* end of file */
  50.     else if (n != MESGHDRSIZE)
  51.         err_sys("message header read error");
  52.  
  53.     /*
  54.      * Read the actual data, if there is any.
  55.      */
  56.  
  57.     if ( (n = mesgptr->mesg_len) > 0)
  58.         if (read(fd, mesgptr->mesg_data, n) != n)
  59.             err_sys("data read error");
  60.     return(n);
  61. }
  62.